package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.AuthorfuliBean;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class AuthorWealActivity extends BaseActivity {
    @BindView(R.id.authorfuli_back)
    ImageView authorfuliBack;
    @BindView(R.id.recycle_authorfuli)
    RecyclerView recycleAuthorfuli;
    private List<AuthorfuliBean.DataBean> datas = new ArrayList<>();
    private AuthorfuliBean authorfulibean;
    private BaseQuickAdapter mAdapter;
    private int selectPosition = -1;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_weal);
        ButterKnife.bind(this);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP + "/sys-welfareBulletin")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            Gson g = new Gson();
                            authorfulibean = g.fromJson(s, AuthorfuliBean.class);
                            if (authorfulibean.getReturnCode() == 200) {
                                datas.addAll(authorfulibean.getData());
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        recycleAuthorfuli.setLayoutManager(new LinearLayoutManager(this));
        recycleAuthorfuli.setAdapter(mAdapter = new BaseQuickAdapter<AuthorfuliBean.DataBean, BaseViewHolder>(R.layout.authorfuli_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, AuthorfuliBean.DataBean item) {

                helper.setText(R.id.fulititle, item.getTitle())
                        .setText(R.id.fulicontext, item.getValues());
                helper.addOnClickListener(R.id.seeall);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(AuthorWealActivity.this,MoreAuthorFuliActivity.class);
                intent.putExtra("title",datas.get(position).getTitle());
                intent.putExtra("cotext",datas.get(position).getContent());
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.authorfuli_back)
    public void onViewClicked() {
        goBack();
    }
}
